﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.Specialized;

namespace VeryCodes.Common
{
    /// <summary>
    /// Request 的摘要说明
    /// </summary>
    public class RequestHelper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RequestHelper"/> class.
        /// </summary>
        public RequestHelper()
        {
        }

        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="paraName"></param>
        /// <returns></returns>
        public static string GetQueryString(string paraName)
        {
            string str = string.Empty;

            if (HttpContext.Current.Request.QueryString[paraName] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[paraName]))
            {
                str = HttpContext.Current.Request.QueryString[paraName].ToString();
                str = str.Replace("'", "''");
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
            }

            return str;
        }

        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="paraName"></param>
        /// <returns></returns>
        public static string GetQueryStringNoFilter(string paraName)
        {
            string str = string.Empty;

            if (HttpContext.Current.Request.QueryString[paraName] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[paraName]))
            {
                str = HttpContext.Current.Request.QueryString[paraName].ToString();
            }

            return str;
        }

        /// <summary>
        /// 获取数字参数
        /// </summary>
        /// <param name="paraName"></param>
        /// <returns></returns>
        public static int GetQueryInt(string paraName)
        {
            string str = string.Empty;
            int strInt = 0;

            if (HttpContext.Current.Request.QueryString[paraName] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[paraName]))
            {
                str = HttpContext.Current.Request.QueryString[paraName].ToString();
            }

            if (!int.TryParse(str, out strInt))
            {
                LogHelper.ToDebug("convert url parameter \"" + paraName + "\" to int failed:" + HttpContext.Current.Request.Path);
            }

            return strInt;
        }

        /// <summary>
        /// 获取字符参数集合
        /// &gt;=VeryCodes.Common 1.0.0.1
        /// </summary>
        /// <value>The query strings.</value>
        /// <returns></returns>
        public static NameValueCollection QueryStrings
        {
            get
            {
                return HttpContext.Current.Request.QueryString;
            }
        }

        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="paraName"></param>
        /// <returns></returns>
        public static string GetFormString(string paraName)
        {
            string str = string.Empty;

            if (HttpContext.Current.Request.Form[paraName] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Form[paraName]))
            {
                str = HttpContext.Current.Request.Form[paraName].ToString();
                str = str.Replace("'", "''");
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
            }

            return str;
        }

        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="paraName"></param>
        /// <returns></returns>
        public static string GetFormStringNoFilter(string paraName)
        {
            string str = string.Empty;

            if (HttpContext.Current.Request.Form[paraName] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Form[paraName]))
            {
                str = HttpContext.Current.Request.Form[paraName].ToString();
            }

            return str;
        }

        /// <summary>
        /// 获取当前登陆IP
        /// </summary>
        /// <returns></returns>
        public string GetIp()
        {
            //可以透过代理服务器
            string userIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (userIP == null || userIP == "")
            {
                //没有代理服务器,如果有代理服务器获取的是代理服务器的IP
                userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            return userIP;
        }
    }
}
